﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PGI.LN;
using PGI.EN.Usuario;
using PGI.LN.Usuario;

namespace PGI.WF
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnIngresar_Click(object sender, EventArgs e)
        {
            try
            {
                UsuarioEn obj = UsuarioLn.Usuario_Login(txtUsuario.Text, txtPassword.Text);

                if (obj != null)
                {
                    this.Hide();
                    
                    MDIPGI frm = MDIPGI.Instance();
                    frm.usuarioForm = obj;
                    frm.Show();
                    frm.Activate();

                    MessageBox.Show(this, "Bienvenido " + obj.Nombre + " " + obj.ApellidoP, "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(this, "Usuario y/o Password inválidos", "Error al ingresar", MessageBoxButtons.OK, MessageBoxIcon.Error);                    
                    txtPassword.SelectAll();
                }

            }
            catch (Exception ex)
            {
                ExceptionLn.enviaError("Error al loguearse", ex);
            }
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnIngresar_Click(sender, e);
            }
            if (e.KeyCode == Keys.Escape)
            {
                btnCerrar_Click(sender, e);
            }
        }

    }
}
